<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\bootstrap\ActiveForm;
use yii\widgets\LinkPager;
use yii\helpers\Url;
$this->title = '课程管理';
?>

<div class="container">
    <div class="row">
     <?php echo $this->render('_leftnav');?> 
        

        <div class="col-md-10">
          <div class="page-header clearfix">
            <h1 class="pull-left">课程管理</h1>
            <div class="pull-right">

               <a class="btn btn-success btn-sm" href="index.php?r=course/create" target="blank">创建课程</a>

                <a class="btn btn-success btn-sm" href="http://dcoffee.edusoho.com.cn/course/create?flag=isLive" target="blank">创建直播课程</a>

                                                
            </div>
          </div>
            <!--<?php echo $this->render('_indexnav') ?>-->
          
            <div class="btn-group">
</div>  <br>
    <!-- <form id="message-search-form" class="form-inline" action="" method="get" novalidate=""> -->
		<?php 
			$form = ActiveForm::begin([
				'options' => ['class' => 'form-inline well well-sm',
				'id'=>'course-form'],
				'action'=>'/index.php?r=course/index',
				'method'=>'post',
				'fieldConfig' => [
					'template' => '<div class="form-group"><div class="col-md-8 controls"  style="padding-top: 9px">{input}{error}</div></div>'
				], 
			]);
		?>
        <!-- <select style="max-width:150px;" class="form-control" name="categoryId">
          <option selected="selected" value="">课程分类</option><option value="3">产品介绍</option><option value="1">使用教程</option><option value="4">　管理员</option><option value="5">　教师</option><option value="6">　学员</option><option value="2">默认分类</option>
        </select> -->
		<?php foreach($type as $k=>$v){
			$types[] = "课程分类";
			$types[$v['type_id']] = $v['type_name'];
		}?>
		<?= Html::dropDownList('coursetype', $coursesearch['coursetype'], ArrayHelper::map($types,'id', 'coursetype'), ['class' => 'form-control coursetype']);?>
        <!-- <select class="form-control" name="status">
          <option selected="selected" value="">课程状态</option><option value="draft">未发布</option><option value="published">已发布</option><option value="closed">已关闭</option>
        </select> -->
		<?php 
			$status=['' => '课程状态','0' => '未发布','1' => '已发布','2' => '已关闭'];
		?>
		<?= Html::dropDownList('status', $coursesearch['status'], ArrayHelper::map($status,'id', 'status'), ['class' => 'form-control status']);?>
		<?= $form->field($model, 'course_name',['inputOptions'=>['placeholder'=>'课程名称','value'=>$coursesearch['Course']['course_name']]])->textInput() ?>
		<?= Html::input('text', 'user_name', $coursesearch['user_name'], ['class' => 'form-control','id' => 'user_name','placeholder' => '创建者']) ?>
		<?= Html::submitButton('搜索', ['class' => 'btn btn-primary']) ?>
	<?php ActiveForm::end(); ?> 
	
  <table class="table table-striped table-hover" id="course-table" style="word-break:break-all;">
    <thead>
    <tr>
      <th>编号</th>
      <th width="30%">名称</th>
      <th width="">连载状态</th>
            <th>学员数</th>
      <th>收入(元)</th>
            <th>状态</th>
      <th>创建者</th>
      <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($courses as $k => $course) {
    
  ?>
                              <tr id="course-tr-<?php echo $course['course_id']; ?>">
  <td><?php echo $course['course_id']; ?></td>
  <td>
    <a href="http://dcoffee.edusoho.com.cn/course/31" target="_blank"><strong><?php echo $course['course_name'] ?></strong></a>
    
    
        <br>
    <span class="text-muted text-sm">分类： <?php echo $course['type']['type_name']  ?></span>
    
  </td>
  <td>
        <span class="text-info"><?php if ($course['is_serialize'] == 0) {
          echo "非连载课程";
        }elseif ($course['is_serialize'] == 1) {
         echo "连载中";
        }else{
          echo "已完结";
          } ?></span>
      </td>
    <td><?php  echo $course['apply_num'] ?></td>

  <td><span class="money-text"><?php if ($course['price_status'] == 0) {
    echo 0.00;
  }elseif ($course['price_status'] == 1) {
    echo $course['course_price']*$course['apply_num'];
  } ?></span></td>
  
  <td><?php if ($course['status'] == 0) {
          echo "<span class='text-muted' id='course".$course['course_id'] ."'>未发布</span>";
        }elseif ($course['status'] == 1) {
         echo "<span class='text-success' id='course".$course['course_id'] ."'>已发布</span>";
        }elseif ($course['status'] == 2){
          echo "<span class='text-danger' id='course".$course['course_id'] ."'>已关闭</span>";
          } ?>
  </td>
  <td>
      		<a href="javascript:;" role="show-user" data-toggle="modal" data-target="#modal" data-url="http://dcoffee.edusoho.com.cn/admin/user/14"><?php echo $course['teacher']['teacher_name']  ?></a>
	
    <br>
    <span class="text-muted text-sm"><?php  echo date('Y-m-d H:i:s',$course['add_time']);  ?></span>
  </td>
  <td>
    <div class="btn-group">
      <a class="btn btn-default btn-sm" href="index.php?r=course/edit&id=<?php echo $course['course_id']; ?>" target="_blank">管理</a>

      <a href="#" type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
        <span class="caret"></span>
      </a>
      <ul class="dropdown-menu pull-right">
      <!--
          <li><a class="recommend-course" href="#modal" data-toggle="modal" data-url="/admin/course/31/recommend?ref=courseList&amp;filter=normal"><span class="glyphicon glyphicon-hand-up"></span> 推荐课程</a></li>
          
          
          <li><a class="copy-course" id="copy-course" href="javascript:" data-toggle="modal" data-target="#modal" data-url="/admin/course/31/copy" data-type="normal"><span class="glyphicon glyphicon-plus-sign"></span> 复制课程</a></li>
        
        <li><a href="http://dcoffee.edusoho.com.cn/course/31?previewAs=guest" target="_blank"><span class="glyphicon glyphicon-eye-open"></span> 预览：作为未购买用户</a></li>
        <li><a href="http://dcoffee.edusoho.com.cn/course/31?previewAs=member" target="_blank"><span class="glyphicon glyphicon-eye-open"></span> 预览：作为已购买用户</a></li>
        -->
        <li class="divider"></li>

            <?php if ($course['status']==0 || $course['status']==2) { ?>
                <li><a class="publish-course" href="javascript:" name="<?php echo $course['course_id']?>" >
                    <span class="glyphicon glyphicon-ok-circle"></span> 发布课程</a>
                  </li>
            <?php }elseif ($course['status']==1) { ?>
                <li><a class="close-course" href="javascript:" name="<?php echo $course['course_id']?>">
                      <span class="glyphicon glyphicon-ban-circle" ></span> 关闭课程</a>
                </li>
            <?php } ?>
            
            <!-- 课程如果发布过，则不可删除   --> 
            <?php if ($course['status']==0) {  ?>
                <li class="divider"></li>
            
                <li><a class="delete-course" href="javascript:" name="<?php echo $course['course_id']; ?>">
                      <span class="glyphicon glyphicon-trash"></span> 删除课程</a>
                </li>
            <?php  } ?>
                
              </ul>
    </div>
  </td>
</tr>
       <?php } ?>                      
          </tbody>
  </table>

    
		<div align="right">
					<?php echo LinkPager::widget([
						'pagination' => $pagination,
						'firstPageLabel'=>"首页",
						'prevPageLabel'=>'上一页',
						'nextPageLabel'=>'下一页',
						'lastPageLabel'=>'尾页',
					]);?>
			</div>
        </div>

        </div>
  </div>


  <script>
    
    $(function() {

        //删除课程
        $(".delete-course").click(function() {
        if(confirm("删除课程，将删除课程的章节、课时、视频、学员信息。真的要删除该课程吗？")){      
            var id = $(this).attr('name');
            $.ajax({
              url: "index.php?r=course/coursedel&id="+id,
              type: 'get',
              dataType: 'html',
             
              success:function(msg){
                if (msg) {
                  $("#course-tr-"+id).remove();
                }
              }
            }) 
          }
        });
      //发布课程
      $(".publish-course").click(function(){
          if(confirm("您确定要发布课程吗？")){
            var id =  $(this).attr('name');
              $.ajax({
                url: "index.php?r=course/course-publish&id="+id,
                type: 'get',
                dataType: 'html',
                
                success:function(msg){                 
                    if (msg==1) {
                        var status = $("#course"+id);
                        status.attr('class', 'text-success');
                        status.text("已发布");
                    }
                }
              })
            }
        });
      //关闭课程
      $(".close-course").click(function(){
          if(confirm("您确定要关闭课程吗？关闭后已购买用户扔可以观看视频课程")){
            var id =  $(this).attr('name');
              $.ajax({
                url: "index.php?r=course/course-close&id="+id, 
                type: 'get',
                dataType: 'html',
                
                success:function(msg){
                    if (msg==1) {
                        var status = $("#course"+id);
                        status.attr('class', 'text-danger');
                        status.text("已关闭");
                    }
                }
              }) 
            }
        });
    })

  </script>